import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import lib.BoardGUI;

public class Othello {

	private static JFrame frmOthello;
	private static JLabel blackLabel;
	private static JLabel whiteLabel;
	private static JPanel boardPanel;
	private static OthelloBoard othelloBoard;
	
	public static void setBlackPoints(int points) {
		blackLabel.setText("Black: "+points);
	}

	public static void setWhitePoints(int points) {
		whiteLabel.setText("White: "+points);
	}
	
	public static void gameOver(Player vencedor){
		String vitoria = "Winner: ";
		if ( vencedor == null)  { vitoria = "Draw!"; }
		else if ( vencedor.equals(Player.BLACK)) { vitoria += "Black"; }
		else if ( vencedor.equals(Player.WHITE)) { vitoria += "White"; }
		
		blackLabel.setOpaque(false);
		whiteLabel.setOpaque(false);
		System.out.println(vitoria);
		JOptionPane.showMessageDialog(boardPanel, vitoria,"Game Over", 3);
	}
	
	public static void setTurnPlayer() {
		boolean isWhite = othelloBoard.getCurrentPlayer().equals(Player.WHITE);
		whiteLabel.setOpaque(isWhite);
		blackLabel.setOpaque(!isWhite);
		frmOthello.getContentPane().repaint();
	}
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Othello window = new Othello();
					window.frmOthello.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Othello() {
		initialize();
	}

	private void initialize() {
		frmOthello = new JFrame();
		frmOthello.setResizable(false);
		frmOthello.setTitle("Othello");
		frmOthello.setBounds(100, 100, 507, 564);
		frmOthello.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmOthello.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(0, 0, 500, 35);
		frmOthello.getContentPane().add(panel);
		panel.setLayout(new GridLayout(0, 3, 0, 0));
		
		JButton newGame = new JButton("New Game");
		newGame.addMouseListener(new MouseAdapter() {
		    public void mouseClicked(MouseEvent evt) {
		    	frmOthello.getContentPane().remove(boardPanel);
		    	boardPanel = createOthelloBoard();
		    	frmOthello.getContentPane().add(boardPanel);
		    	blackLabel.setText("Black: 0");
		    	whiteLabel.setText("White: 0");
		    	frmOthello.getContentPane().repaint();
		    }
		});
		panel.add(newGame);
		
		blackLabel = new JLabel("Black: 2");
		blackLabel.setHorizontalAlignment(SwingConstants.CENTER);
		blackLabel.setBackground(Color.lightGray);
		panel.add(blackLabel);
		
		whiteLabel = new JLabel("White: 2");
		whiteLabel.setHorizontalAlignment(SwingConstants.CENTER);
		whiteLabel.setBackground(Color.lightGray);
		panel.add(whiteLabel);
		
		boardPanel = createOthelloBoard();
		frmOthello.getContentPane().add(boardPanel);

		setTurnPlayer();
	}
	
	private JPanel createOthelloBoard() {
		othelloBoard = new OthelloBoard();
		BoardGUI boardGUI = new BoardGUI(othelloBoard);
		boardGUI.setBoardRenderProperties (new OthelloBoardRenderProperties());
		boardGUI.addBoardEventListener (new OthelloBoardEventListener());
		boardGUI.setBounds(0, 35, 500, 500);
		return boardGUI;
	}

}